<template>
  <div class="login-container">
    <!-- 登录内容区域 -->
    <div class="login-form-wrap">
      <!-- logo -->
      <div class="login-head">
        <div class="logo"></div>
      </div>
      <!--element-ui  表单组件 -->
      <el-form class="login-form" ref="loginForm" :model="form" :rules="rules">
        <el-form-item prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入手机号"></el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-input v-model="form.code" placeholder="请输入验证码"></el-input>
        </el-form-item>
        <el-form-item prop="isChecked">
          <el-checkbox v-model="form.isChecked"
            >我已阅读并同意用户协议和隐私条款</el-checkbox
          >
        </el-form-item>
        <el-form-item>
          <el-button class="login-btn" type="primary" @click="login"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
// 导入登录后台接口
import { login } from '@/api/user'
export default {
  data () {
    // 定义变量和方法,校验验证码
    const validateCode = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('验证码不能为空！'))
      } else if (value.length !== 6) {
        callback(new Error('验证码长度是6位'))
      } else {
        callback()
      }
    }

    // 校验用户是否同意用户协议
    const validateChecked = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请勾选用户协议！'))
      } else {
        callback()
      }
    }

    // 返回响应数据
    return {
      // 表单数据
      form: {
        // 手机号
        mobile: '13911111111',
        // 验证码
        code: '246810',
        // 同意协议
        isChecked: false
      },
      // 表单验证规则
      rules: {
        mobile: [
          // 校验手机号是否为空
          { required: true, message: '请输入手机号', trigger: 'blur' },
          // 校验手机号格式
          { min: 11, max: 11, message: '请输入11位手机号', trigger: 'blur' }
        ],
        code: [
          // 自定义校验
          { validator: validateCode, trigger: 'blur' }
        ],
        isChecked: [
          // 自定义校验
          { validator: validateChecked, trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    // 登录、提交
    login () {
      console.log(this.$refs.loginForm)

      this.$refs.loginForm.validate(async (valid) => {
        if (valid) {
          const { mobile, code } = this.form
          // 调用登录接口
          const res = await login(mobile, code)
          console.log(res)
          this.$router.replace('/')
        } else {
          console.warn('请检查！')
          return false
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.login-container {
  position: fixed;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  background: url("../../assets/img/login_bg.jpg") no-repeat;
  background-size: cover;
  .login-form-wrap {
    min-width: 300px;
    padding: 30px 50px 10px;
    background-color: #fff;
    .login-head {
      display: flex;
      justify-content: center;
      .logo {
        width: 200px;
        height: 57px;
        background: url("../../assets/img/logo_index.png") no-repeat;
        background-size: contain;
      }
    }
    /deep/ .el-form-item__content {
      text-align: left;
    }
    .login-form {
      .login-btn {
        width: 100%;
      }
    }
  }
}
</style>
